package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.EscapeUtil;
import cn.hutool.core.util.PageUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.google.api.client.util.SecurityUtils;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.tb.lmt.domain.TbRepairplanlist;
import com.ruoyi.tb.lmt.domain.TbWorktemplate;
import com.ruoyi.tb.lmt.domain.bo.*;
import com.ruoyi.tb.lmt.domain.vo.*;
import com.ruoyi.tb.lmt.mapper.*;
import com.ruoyi.tb.lmt.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.TbLmtrepairWorkdetail;


import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 检修工作Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-14
 */
@RequiredArgsConstructor
@Service
public class TbLmtrepairWorkdetailServiceImpl implements ITbLmtrepairWorkdetailService {

    private final TbLmtrepairWorkdetailMapper baseMapper;

    private final OilSampleDetectionMapper oilSampleDetectionMapper;

    private final DrDamperDataMapper drDamperDataMapper;

    @Autowired
    private ITbRepairplanlistService iTbRepairplanlistService;

    @Autowired
    private ITbWorktemplateService tbWorktemplateService;

    @Autowired
    private TbLmtrepairWorkdetailMapper tbLmtrepairWorkdetailMapper;

    @Autowired
    private TbRepairplanlistMapper tbRepairplanlistMapper;

    @Autowired
    private TbLmtrepairCheckdetailMapper tbLmtrepairCheckdetailMapper;

    @Autowired
    private TbLocomotiveMapper tbLocomotiveMapper;

    @Autowired
    private TEquipmentMapper equipmentMapper;

    @Autowired
    private TbWorkFileMapper tbWorkFileMapper;

    private final JdbcOperations jdbcTemplate;

    @Autowired
    private ITbMutualInspectionService iTbMutualInspectionService;

    @Autowired
    private TbRepairplanMapper tbRepairplanMapper;

    @Autowired
    private TbStorageMapper tbStorageMapper;

    @Autowired
    private ITbSpecialInspectionService iTbSpecialInspectionService;

    @Autowired
    private ITbLocomotiveService iTbLocomotiveService;
    @Autowired
    private ITbLmttypeService iTbLmttypeService;

    /**
     * 查询检修工作
     */
    @Override
    public TbLmtrepairWorkdetailVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询检修工作列表
     */
    @Override
    public TableDataInfo<TbLmtrepairWorkdetailVo> queryPageList(TbLmtrepairWorkdetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TbLmtrepairWorkdetail> lqw = buildQueryWrapper(bo);
        Page<TbLmtrepairWorkdetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询检修工作列表
     */
    @Override
    public List<TbLmtrepairWorkdetailVo> queryList(TbLmtrepairWorkdetailBo bo) {
        LambdaQueryWrapper<TbLmtrepairWorkdetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public List<TbLmtrepairWorkdetailVo> queryListAndNotIds(String like,String like1, List<String> ids) {
        return baseMapper.queryListAndNotIds(like,like,ids);
    }

    private LambdaQueryWrapper<TbLmtrepairWorkdetail> buildQueryWrapper(TbLmtrepairWorkdetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TbLmtrepairWorkdetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSendUser()), TbLmtrepairWorkdetail::getSendUser, bo.getSendUser());
        lqw.between(params.get("beginSendDate") != null && params.get("endSendDate") != null, TbLmtrepairWorkdetail::getSendDate, params.get("beginSendDate"), params.get("endSendDate"));
        return lqw;
    }

    private LambdaQueryWrapper<TbLmtrepairWorkdetail> buildQueryWrapper1(String id, String status) {
        LambdaQueryWrapper<TbLmtrepairWorkdetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(id), TbLmtrepairWorkdetail::getLmtRepairId, id);
        queryWrapper.eq(StringUtils.isNotBlank(status), TbLmtrepairWorkdetail::getStatus, status);
        return queryWrapper;
    }

    /**
     * 新增检修工作
     */
    @Override
    public Boolean insertByBo(TbLmtrepairWorkdetailBo bo) {
        TbLmtrepairWorkdetail add = BeanUtil.toBean(bo, TbLmtrepairWorkdetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }


    @Override
    public Long insertByBoReturnId(TbLmtrepairWorkdetailBo bo) {
        TbLmtrepairWorkdetail add = BeanUtil.toBean(bo, TbLmtrepairWorkdetail.class);
        validEntityBeforeSave(add);
        // 假设 baseMapper.insert(add) 方法在插入后能够使得 add 对象包含生成的ID
        if (baseMapper.insert(add) > 0) {
            // 直接返回新增记录的ID
            bo.setId(add.getId());
            return add.getId();
        } else {
            throw new RuntimeException("添加失败"); // 插入失败时抛出异常
        }
    }

    /**
     * 修改检修工作
     */
    @Override
    public Boolean updateByBo(TbLmtrepairWorkdetailBo bo) {
        TbLmtrepairWorkdetail update = BeanUtil.toBean(bo, TbLmtrepairWorkdetail.class);
        validEntityBeforeSave(update);

        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TbLmtrepairWorkdetail entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除检修工作
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean startByBo(TbRepairplanlistBo bo) {
        //查询年检内容
        TbLocomotiveVo tbLocomotiveVo = iTbLocomotiveService.selectLocomotive(bo.getLmtNumber());
        TbRepairplanlistBo bo2 = new TbRepairplanlistBo();
        bo2.setLmtType(tbLocomotiveVo.getLmtType());
        bo2.setRepairType(bo.getRepairType());
        List<TbWorktemplateVo> list = tbWorktemplateService.selectListByTbRepairplanlistBo(bo2);
        if (list.size() < 1) {
            throw new RuntimeException(bo.getLmtType()+":"+bo.getLmtNumber()+"的"+bo.getRepairType()+"检修内容为空，请核对基础数据中的检修内容管理");
        }
        List<TbLmtrepairWorkdetail> lmtrepairWorkdetailList = new ArrayList<>();
        for (TbWorktemplateVo tbWorktemplateVo : list) {

            TbLmtrepairWorkdetail tbLmtrepairWorkdetail = new TbLmtrepairWorkdetail();

            // 设置各个字段值
            tbLmtrepairWorkdetail.setLmtRepairId(bo.getId());
            tbLmtrepairWorkdetail.setStartTime(bo.getStartTime());
            tbLmtrepairWorkdetail.setEndTime(bo.getEndTime());
            tbLmtrepairWorkdetail.setRepairStation(bo.getRepairStation());

            tbLmtrepairWorkdetail.setRemark(bo.getRemark());
            tbLmtrepairWorkdetail.setWorkKind(tbWorktemplateVo.getWorkKind());
            tbLmtrepairWorkdetail.setWorkCategory(tbWorktemplateVo.getWorkCategory());
            tbLmtrepairWorkdetail.setWorkCategoryId(tbWorktemplateVo.getWorkCategoryId());
            tbLmtrepairWorkdetail.setIszdf(tbWorktemplateVo.getIszdf());
            tbLmtrepairWorkdetail.setIsts(tbWorktemplateVo.getIsts());
            tbLmtrepairWorkdetail.setIsmustcheck(tbWorktemplateVo.getIsmustcheck());
            tbLmtrepairWorkdetail.setIsmustcheck2(tbWorktemplateVo.getIsmustcheck2());
            tbLmtrepairWorkdetail.setWorkType(bo.getRepairType());
            tbLmtrepairWorkdetail.setWorkName(tbWorktemplateVo.getWorkName());
            tbLmtrepairWorkdetail.setWorkRequire(tbWorktemplateVo.getWorkRequire());
            tbLmtrepairWorkdetail.setOrderNo(tbWorktemplateVo.getOrderNo());
            tbLmtrepairWorkdetail.setWorkTimes(bo.getRepairTimes());
            tbLmtrepairWorkdetail.setWorkMethod(tbWorktemplateVo.getWorkMode());
            tbLmtrepairWorkdetail.setWorkDirectName(tbWorktemplateVo.getWorkDirectName());
            tbLmtrepairWorkdetail.setInspectionTopic(tbWorktemplateVo.getInspectionTopic());
            tbLmtrepairWorkdetail.setWorkDirectUrl(tbWorktemplateVo.getWorkDirectUrl());
            tbLmtrepairWorkdetail.setStatus("未下达");
            tbLmtrepairWorkdetail.setFormConfig(tbWorktemplateVo.getFormConfig());
            tbLmtrepairWorkdetail.setPrintConfig(tbWorktemplateVo.getPrintConfig());
            tbLmtrepairWorkdetail.setPreWorkIndex(tbWorktemplateVo.getPreWorkIndex());
            tbLmtrepairWorkdetail.setIsTestBed(tbWorktemplateVo.getIsTestBed());
            tbLmtrepairWorkdetail.setWorkGroup(tbWorktemplateVo.getWorkGroup());
            tbLmtrepairWorkdetail.setWorkGroupId(tbWorktemplateVo.getWorkGroupId());
            lmtrepairWorkdetailList.add(tbLmtrepairWorkdetail);
        }
        //二级修不生成互检表数据
        if (bo.getRepairType().equals("三级修") || bo.getRepairChar().equals("四级修")) {
            TbMutualInspectionBo bos = new TbMutualInspectionBo();
            bos.setLmtRepairId(bo.getId());
            bos.setPlanId(bo.getPlanId());
            bos.setPlanName(bo.getPlanName());
            bos.setLmtType(bo.getLmtType());
            bos.setLmtNumber(bo.getLmtNumber());
            bos.setWorkerTeam(bo.getWorkerTeam());
            bos.setWorkGroup(bo.getWorkGroup());
            bos.setWorkGroupId(bo.getWorkGroupId());
            bos.setRepairChar(bo.getRepairChar());
            bos.setRepairType(bo.getRepairType());
            bos.setRepairTimes(bo.getRepairTimes());
            bos.setRepairCode(bo.getRepairCode());
            //设置时间
            String dateString = bo.getRepairStartDate(); // 获取字符串类型的日期
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // 日期字符串是这种格式
            LocalDate date = LocalDate.parse(dateString, formatter); // 将字符串转换为LocalDate
            // 如果bos.setRepairStartDate() 期望的是 java.util.Date 类型，则需要转换
            Date dateConverted = Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant());
            bos.setRepairStartDate(dateConverted); // 设置日期
            //设置完实体类，调用互检新增方法
            TbMutualInspectionBo bo1 = iTbMutualInspectionService.selectByBo(bos);
            iTbMutualInspectionService.insertByBo(bo1);
        }
        //二级修生成专检表数据
        if (bo.getRepairType().equals("二级修")) {
            TbSpecialInspectionBo bos = new TbSpecialInspectionBo();
            bos.setLmtRepairId(bo.getId());
            bos.setPlanId(bo.getPlanId());
            bos.setPlanName(bo.getPlanName());
            bos.setLmtType(bo.getLmtType());
            bos.setLmtNumber(bo.getLmtNumber());
            bos.setWorkerTeam(bo.getWorkerTeam());
            bos.setWorkGroup(bo.getWorkGroup());
            bos.setWorkGroupId(bo.getWorkGroupId());
            bos.setRepairChar(bo.getRepairChar());
            bos.setRepairType(bo.getRepairType());
            bos.setRepairTimes(bo.getRepairTimes());
            bos.setRepairCode(bo.getRepairCode());
            //设置时间
            String dateString = bo.getRepairStartDate(); // 获取字符串类型的日期
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // 日期字符串是这种格式
            LocalDate date = LocalDate.parse(dateString, formatter); // 将字符串转换为LocalDate
            // 如果bos.setRepairStartDate() 期望的是 java.util.Date 类型，则需要转换
            Date dateConverted = Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant());
            bos.setRepairStartDate(dateConverted); // 设置日期
            //设置完实体类，调用互检新增方法
            TbSpecialInspectionBo bo1 = iTbSpecialInspectionService.selectByBo(bos);
            iTbSpecialInspectionService.insertByBo(bo1);
        }

        //设置目标大机状态为检修中
        Long lmtId = iTbLocomotiveService.selectByLmtId(bo.getId());
        TbLocomotiveBo lmtBo = new TbLocomotiveBo();
        lmtBo.setID(lmtId);
        lmtBo.setStatus("检修中");
        iTbLocomotiveService.updateByBo(lmtBo);

        //批量插入
        return baseMapper.insertBatch(lmtrepairWorkdetailList);
    }

    @Override
    public TableDataInfo<TbLmtrepairWorkdetailVo> selectWorkList(String id, String status, String workKind, String workCategory, String workName, String workRequire, String workGroup, PageQuery pageQuery) {
        //查询符合条件的工作表内容
        List<TbLmtrepairWorkdetailVo> result = tbLmtrepairWorkdetailMapper.selectWorkByRepairId(id, status, workKind, workCategory, workName, workRequire, workGroup);
        //分页
        if (pageQuery.getPageSize() != null && pageQuery.getPageNum() != null) {
            List<TbLmtrepairWorkdetailVo> page = ListUtil.page(pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);
            TableDataInfo res = new TableDataInfo<>(page, result.size());
            return res;
        } else {
            return TableDataInfo.build(result);
        }
    }

    @Override
    public List<TbLmtrepairWorkdetailVo> selectWorkList(String id, String status, String lmtType, String lmtNumber, String repairChar) {
        return tbLmtrepairWorkdetailMapper.selectByRepairId(id, status, lmtType, lmtNumber, repairChar);
    }

    @Override
    public TableDataInfo<TbLmtrepairWorkdetailVo> selectWorkDispatchList(
        String id, String status, String workSelect, String lmtType,
        String lmtNumber, String repairChar, String workKind, PageQuery pageQuery) {

        List<TbLmtrepairWorkdetailVo> result = new ArrayList<>();
        // 获取当前用户的工队id
        if (workSelect.equals("本工队工作")) {
            LoginUser loginUser = LoginHelper.getLoginUser();
            Long workGroupId = loginUser.getDeptId();
            // 获取工队及其所有子工队的ID列表
            List<Long> deptIds = getDepartmentAndChildren(workGroupId);
            // 查询符合条件的工作表内容
            result = tbLmtrepairWorkdetailMapper.selectDispatchByRepairIdAndDeptIds(
                id, status, deptIds, lmtType, lmtNumber, workKind, repairChar);

        } else if (workSelect.equals("所有工作")) {
            // 查询符合条件的工作表内容
            result = tbLmtrepairWorkdetailMapper.selectDispatchByRepairId(
                id, status, null, lmtType, lmtNumber, workKind, repairChar);
        }

        // 分页
        if (pageQuery.getPageSize() != null && pageQuery.getPageNum() != null) {
            List<TbLmtrepairWorkdetailVo> page = ListUtil.page(
                pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);
            TableDataInfo res = new TableDataInfo<>(page, result.size());
            return res;
        } else {
            return TableDataInfo.build(result);
        }
    }


    //查询当前用户的所有子部门ID列表
    public List<Long> getDepartmentAndChildren(Long deptId) {
        List<Long> departmentIds = new ArrayList<>();
        departmentIds.add(deptId);
        findChildDepartmentIds(deptId, departmentIds);
        return departmentIds;
    }

    private void findChildDepartmentIds(Long parentId, List<Long> departmentIds) {
        List<Long> childIds = jdbcTemplate.queryForList(
            "SELECT dept_id FROM sys_dept WHERE parent_id = ?",
            Long.class,
            parentId
        );

        for (Long childId : childIds) {
            departmentIds.add(childId);
            findChildDepartmentIds(childId, departmentIds);
        }
    }


    @Override
    public TableDataInfo<TbLmtrepairWorkdetailVo> selectWorkOverhaulList(String id, String status, String workSelect, String lmtType, String lmtNumber, String repairChar, String workCategory, PageQuery pageQuery) {

        List<TbLmtrepairWorkdetailVo> result = new ArrayList<>();
        //根据前端传来的参数查询对应的工作表内容
        if (workSelect.equals("自己的工作")) {
            LoginUser loginUser = LoginHelper.getLoginUser();
            String workUser = loginUser.getUsername();
            //查询符合条件的工作表内容
            result = tbLmtrepairWorkdetailMapper.selectOverhaulByRepairId(id, status, workUser, null, lmtType, lmtNumber, repairChar, workCategory);
        } else if (workSelect.equals("本工队任务")) {
            LoginUser loginUser = LoginHelper.getLoginUser();
            Long workGroupId = loginUser.getDeptId();
            // 获取工队及其所有子工队的ID列表
            List<Long> deptIds = getDepartmentAndChildren(workGroupId);
            //查询符合条件的工作表内容
            result = tbLmtrepairWorkdetailMapper.selectOverhaulByRepairId(id, status, null, deptIds, lmtType, lmtNumber, repairChar, workCategory);
        } else if (workSelect.equals("全部任务")) {
            //查询符合条件的工作表内容
            result = tbLmtrepairWorkdetailMapper.selectOverhaulByRepairId(id, status, null, null, lmtType, lmtNumber, repairChar, workCategory);
        }
        if (pageQuery.getPageSize() != null && pageQuery.getPageNum() != null) {
            List<TbLmtrepairWorkdetailVo> page = ListUtil.page(pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);
            TableDataInfo res = new TableDataInfo<>(page, result.size());
            return res;
        } else {
            return TableDataInfo.build(result);
        }

    }

    @Override
    public Boolean updateWork(TbLmtrepairWorkdetailBo bo) {
        TbLmtrepairWorkdetail update = BeanUtil.toBean(bo, TbLmtrepairWorkdetail.class);
        validEntityBeforeSave(update);
        LambdaUpdateWrapper<TbLmtrepairWorkdetail> luw = Wrappers.lambdaUpdate();
        luw.set(TbLmtrepairWorkdetail::getWorkGroupId, bo.getWorkGroupId());
        luw.set(TbLmtrepairWorkdetail::getWorkGroup2Id, bo.getWorkGroup2Id());
        luw.set(TbLmtrepairWorkdetail::getWorkGroup, bo.getWorkGroup());
        luw.set(TbLmtrepairWorkdetail::getWorkGroup2, bo.getWorkGroup2());
        luw.set(TbLmtrepairWorkdetail::getStartRequireDate, bo.getStartRequireDate());
        luw.set(TbLmtrepairWorkdetail::getWorkType, bo.getWorkType());
        luw.set(TbLmtrepairWorkdetail::getWorkName, bo.getWorkName());
        luw.set(TbLmtrepairWorkdetail::getWorkRequire, bo.getWorkRequire());
        luw.set(TbLmtrepairWorkdetail::getIsTestBed, bo.getIsTestBed());
        luw.eq(TbLmtrepairWorkdetail::getId, bo.getId());
        return baseMapper.update(luw) > 0;
    }


    //遗留问题右侧列表
    @Override
    public TableDataInfo<TbLegacyIssuesVo> selectWorkLegacyList(String id, String workSelect, String lmtType, String lmtNumber, String repairChar, PageQuery pageQuery) {

        List<TbLegacyIssuesVo> result = new ArrayList<>();
        //根据前端传来的参数查询对应的工作表内容
        if (workSelect.equals("本工队任务")) {
            LoginUser loginUser = LoginHelper.getLoginUser();
            Long workGroupId = loginUser.getDeptId();
            // 获取工队及其所有子工队的ID列表
            List<Long> deptIds = getDepartmentAndChildren(workGroupId);
            //查询符合条件的工作表内容
            result = tbLmtrepairWorkdetailMapper.selectLegacyByRepairId(id, deptIds, lmtType, lmtNumber, repairChar);
        } else if (workSelect.equals("全部任务")) {
            //查询符合条件的工作表内容
            result = tbLmtrepairWorkdetailMapper.selectLegacyByRepairId(id, null, lmtType, lmtNumber, repairChar);
        }
        //分页
        if (pageQuery.getPageSize() != null && pageQuery.getPageNum() != null) {
            List<TbLegacyIssuesVo> page = ListUtil.page(pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);
            TableDataInfo res = new TableDataInfo<>(page, result.size());
            return res;
        } else {
            return TableDataInfo.build(result);
        }

    }


    //过程检查右侧列表
    @Override
    public TableDataInfo<TbLmtrepairWorkdetailVo> selectWorkCheckList(String id, String workSelect, String lmtType, String lmtNumber, String repairChar, PageQuery pageQuery) {

        List<TbLmtrepairWorkdetailVo> result = new ArrayList<>();
        //根据前端传来的参数查询对应的工作表内容
        if (workSelect.equals("本工队任务")) {
            LoginUser loginUser = LoginHelper.getLoginUser();
            Long workGroupId = loginUser.getDeptId();
            // 获取工队及其所有子工队的ID列表
            List<Long> deptIds = getDepartmentAndChildren(workGroupId);
            //查询符合条件的工作表内容
            result = tbLmtrepairWorkdetailMapper.selectCheckByRepairId(id, deptIds, lmtType, lmtNumber, repairChar);
        } else if (workSelect.equals("全部任务")) {
            //查询符合条件的工作表内容
            result = tbLmtrepairWorkdetailMapper.selectCheckByRepairId(id, null, lmtType, lmtNumber, repairChar);
        }
        //分页
        if (pageQuery.getPageSize() != null && pageQuery.getPageNum() != null) {
            List<TbLmtrepairWorkdetailVo> page = ListUtil.page(pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);
            TableDataInfo res = new TableDataInfo<>(page, result.size());
            return res;
        } else {
            return TableDataInfo.build(result);
        }
    }

    //过程验收右侧列表
    @Override
    public TableDataInfo<TbLmtrepairWorkdetailVo> selectWorkApprovalList(String id, String workSelect, String lmtType, String lmtNumber, String repairChar, PageQuery pageQuery) {

        List<TbLmtrepairWorkdetailVo> result = new ArrayList<>();
        //根据前端传来的参数查询对应的工作表内容
        if (workSelect.equals("质检组必检")) {
            LoginUser loginUser = LoginHelper.getLoginUser();
            Long workGroupId = loginUser.getDeptId();
            //查询符合条件的工作表内容
            result = tbLmtrepairWorkdetailMapper.selectApprovalByRepairId(id, null, workSelect, lmtType, lmtNumber, repairChar);
        } else if (workSelect.equals("局必检")) {
            //查询符合条件的工作表内容
            LoginUser loginUser = LoginHelper.getLoginUser();
            Long workGroupId = loginUser.getDeptId();
            result = tbLmtrepairWorkdetailMapper.selectApprovalByRepairId(id, null, workSelect, lmtType, lmtNumber, repairChar);
        }
        //分页
        if (pageQuery.getPageSize() != null && pageQuery.getPageNum() != null) {
            List<TbLmtrepairWorkdetailVo> page = ListUtil.page(pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);
            TableDataInfo res = new TableDataInfo<>(page, result.size());
            return res;
        } else {
            return TableDataInfo.build(result);
        }

    }

    @Override
    public List<TbLmtrepairWorkdetailVo> selectWorkHistoryList(String id, String workSelect, String lmtType, String lmtNumber, String repairChar) {
        List<TbLmtrepairWorkdetailVo> result = new ArrayList<>();
        //根据前端传来的参数查询对应的工作表内容
        if (workSelect.equals("质检组必检")) {
            LoginUser loginUser = LoginHelper.getLoginUser();
            Long workGroupId = loginUser.getDeptId();
            //查询符合条件的工作表内容
            result = tbLmtrepairWorkdetailMapper.selectApprovalByRepairId(id, null, workSelect, lmtType, lmtNumber, repairChar);
        } else if (workSelect.equals("局必检")) {
            //查询符合条件的工作表内容
            LoginUser loginUser = LoginHelper.getLoginUser();
            Long workGroupId = loginUser.getDeptId();
            result = tbLmtrepairWorkdetailMapper.selectApprovalByRepairId(id, null, workSelect, lmtType, lmtNumber, repairChar);
        }
        return result;
    }

    @Override
    public TableDataInfo<TbLmtrepairWorkdetailVo> selectWorkHistoryList(String id, String workKind, String workGroup, String workSelect, String status, Date startTime, PageQuery pageQuery) {
        List<TbLmtrepairWorkdetailVo> result = new ArrayList<>();
        //根据前端传来的参数查询对应的工作表内容
        if (workSelect.equals("检修内容")) {
            //查询符合条件的工作表内容
            result = tbLmtrepairWorkdetailMapper.selectWorkHistoryList(id, workKind, workGroup, status, startTime);
        } else if (workSelect.equals("验收问题")) {
            //查询符合条件的工作表内容
            result = tbLmtrepairCheckdetailMapper.selectWorkHistoryList(id, workKind, workGroup, status, startTime);
        } else if (workSelect.equals("检修资料")) {
            result = tbWorkFileMapper.selectWorkFile(id);
        }

        //分页
        if (pageQuery.getPageSize() != null && pageQuery.getPageNum() != null) {
            List<TbLmtrepairWorkdetailVo> page = ListUtil.page(pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);
            TableDataInfo res = new TableDataInfo<>(page, result.size());
            return res;
        } else {
            return TableDataInfo.build(result);
        }
    }

    @Override
    public List<TbLmtrepairWorkdetailVo> selectHistoryList(String id, String workKind, String workGroup, String workSelect, String status, Date startTime) {
        return tbLmtrepairWorkdetailMapper.selectWorkHistoryList(id, workKind, workGroup, status, startTime);
    }

    @Override
    public Map<String, Object> queryMapById(Long id) {
        return baseMapper.queryMapById(id);
    }

    @Override
    public List<TbLmtrepairWorkdetailVo> selectWorkInfoList(Long id) {
        List<TbLmtrepairWorkdetailVo> result = tbLmtrepairWorkdetailMapper.selectWorkInfoList(id);

        // 对每个结果进行处理，去除转义字符
        for (TbLmtrepairWorkdetailVo item : result) {
            String formConfig = item.getFormConfig();
            // 使用 Hutool 的 EscapeUtil 反转义 JSON 字符串
            String unescapedFormConfig = EscapeUtil.unescape(formConfig);
            // 更新结果对象中的字段值
            item.setFormConfig(unescapedFormConfig);
        }

        return result;
    }

    @Override
    public TableDataInfo<TbLmtrepairWorkdetailVo> selectTaskAllById(String lmtRepairId, String lmtType, String lmtNumber, String status, String repairChar, PageQuery pageQuery) {
        List<TbLmtrepairWorkdetailVo> result = tbLmtrepairWorkdetailMapper.selectTaskAllById(lmtRepairId, lmtType, lmtNumber, status, repairChar);
        //分页
        if (pageQuery.getPageSize() != null && pageQuery.getPageNum() != null) {
            List<TbLmtrepairWorkdetailVo> page = ListUtil.page(pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);
            TableDataInfo res = new TableDataInfo<>(page, result.size());
            return res;
        } else {
            return TableDataInfo.build(result);
        }
    }

    @Override
    public TableDataInfo<TbLmtrepairWorkdetailVo> selectMutualById(String lmtRepairId, String lmtType, String lmtNumber, String status, String repairChar, PageQuery pageQuery) {
        List<TbLmtrepairWorkdetailVo> result = tbLmtrepairWorkdetailMapper.selectMutualById(lmtRepairId, lmtType, lmtNumber, status, repairChar);
        //分页
        if (pageQuery.getPageSize() != null && pageQuery.getPageNum() != null) {
            List<TbLmtrepairWorkdetailVo> page = ListUtil.page(pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);
            TableDataInfo res = new TableDataInfo<>(page, result.size());
            return res;
        } else {
            return TableDataInfo.build(result);
        }
    }

    @Override
    public TableDataInfo<TbLmtrepairWorkdetailVo> selectSpecialById(String lmtRepairId, String lmtType, String lmtNumber, String status, String repairChar, PageQuery pageQuery) {
        List<TbLmtrepairWorkdetailVo> result = tbLmtrepairWorkdetailMapper.selectSpecialById(lmtRepairId, lmtType, lmtNumber, status, repairChar);
        //分页
        if (pageQuery.getPageSize() != null && pageQuery.getPageNum() != null) {
            List<TbLmtrepairWorkdetailVo> page = ListUtil.page(pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);
            TableDataInfo res = new TableDataInfo<>(page, result.size());
            return res;
        } else {
            return TableDataInfo.build(result);
        }
    }

    @Override
    public TableDataInfo<TbLmtrepairWorkdetailVo> selectAcceptanceById(String lmtRepairId, String lmtType, String lmtNumber, String status, String repairChar, PageQuery pageQuery) {
        List<TbLmtrepairWorkdetailVo> result = tbLmtrepairWorkdetailMapper.selectAcceptanceById(lmtRepairId, lmtType, lmtNumber, status, repairChar);
        //分页
        if (pageQuery.getPageSize() != null && pageQuery.getPageNum() != null) {
            List<TbLmtrepairWorkdetailVo> page = ListUtil.page(pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);
            TableDataInfo res = new TableDataInfo<>(page, result.size());
            return res;
        } else {
            return TableDataInfo.build(result);
        }
    }

    @Override
    public Boolean isFinishByLmtId(String lmtRepairId) {
        return tbLmtrepairWorkdetailMapper.isFinishByLmtId(lmtRepairId);
    }

    @Override
    public Boolean isIssuesByLmtId(String lmtRepairId) {
        return tbLmtrepairWorkdetailMapper.isIssuesByLmtId(lmtRepairId);
    }

    @Override
    public TableDataInfo<TbDigitalLmtVo> selectDigitalLmt(String lmtNumber, PageQuery pageQuery) {
        List<TbDigitalLmtVo> result = tbLocomotiveMapper.selectDigitalLmt(lmtNumber);


        for (TbDigitalLmtVo lmt : result) {
            Long lmtRepairId = tbRepairplanlistMapper.selectLmtId(lmt.getLmtType(), lmt.getLmtNumber());
            lmt.setLmtRepairId(lmtRepairId);
            TbRepairplanlist planList = tbRepairplanlistMapper.selectById(lmtRepairId);
            if (planList != null) {
                // 仅当planList不为null时，才尝试访问其属性
                String repairType = planList.getRepairType();
                if (repairType != null) {
                    lmt.setRepairType(repairType);
                }

                String status = planList.getStatus();
                if (status != null) {
                    lmt.setOverhaulProgress(status);
                }
            }

            List<EquipmentVo> equipmentList = equipmentMapper.selectEquipmentByLmtNumber(lmt.getLmtNumber());
            lmt.setEquipmentList(equipmentList);
        }
        //分页
        if (pageQuery.getPageSize() != null && pageQuery.getPageNum() != null) {
            List<TbDigitalLmtVo> page = ListUtil.page(pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);
            TableDataInfo res = new TableDataInfo<>(page, result.size());
            return res;
        } else {
            return TableDataInfo.build(result);
        }
    }


    @Override
    public TableDataInfo<EquipmentVo> selectEquipment(String lmtNumber, PageQuery pageQuery) {
        List<EquipmentVo> result = tbLocomotiveMapper.selectEquipment(lmtNumber);

        //分页
        if (pageQuery.getPageSize() != null && pageQuery.getPageNum() != null) {
            List<EquipmentVo> page = ListUtil.page(pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);
            TableDataInfo res = new TableDataInfo<>(page, result.size());
            return res;
        } else {
            return TableDataInfo.build(result);
        }
    }

    //已提交提料列表
    @Override
    public TableDataInfo<TbLmtrepairWorkdetailVo> selectPickupList(PageQuery pageQuery) {
        List<TbLmtrepairWorkdetailVo> result = tbLmtrepairWorkdetailMapper.selectPickupList();
        //分页
        if (pageQuery.getPageSize() != null && pageQuery.getPageNum() != null) {
            List<TbLmtrepairWorkdetailVo> page = ListUtil.page(pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);
            TableDataInfo res = new TableDataInfo<>(page, result.size());
            return res;
        } else {
            return TableDataInfo.build(result);
        }
    }

    @Override
    public DigitalDateVo selectDate(String lmtNumber) {

        List<OilSampleDetectionVo> oilList = oilSampleDetectionMapper.selectOil(lmtNumber, null);

        List<DrDamperDataVo> drDamperDataList = drDamperDataMapper.selectDamperData(null, null, lmtNumber);

        DigitalDateVo digitalDateVo = new DigitalDateVo();
        digitalDateVo.setOilList(oilList);
        digitalDateVo.setDamperList(drDamperDataList);

        return digitalDateVo;
    }

    @Override
    public AcceptCountVo selectAcceptCount() {
        AcceptCountVo acceptCountVo = new AcceptCountVo();
        //找到当前用户
        LoginUser loginUser = LoginHelper.getLoginUser();
        String workUser = loginUser.getUsername();
        String workGroup = loginUser.getDeptName();
        Long workGroupId = loginUser.getDeptId();

        // 获取工队及其所有子工队的ID列表
        List<Long> deptIds = getDepartmentAndChildren(workGroupId);

        acceptCountVo.setAllCount(tbLmtrepairWorkdetailMapper.selectAllCount(deptIds));

        acceptCountVo.setNotStartCount(tbLmtrepairWorkdetailMapper.selectNotStartCount(deptIds));

        acceptCountVo.setNotFinishCount(tbLmtrepairWorkdetailMapper.selectNotFinishCount(deptIds));

        acceptCountVo.setFinishCount(tbLmtrepairWorkdetailMapper.selectFinishCount(deptIds));

        acceptCountVo.setIssuesCount(tbLmtrepairWorkdetailMapper.selectIssuesCount(deptIds));

        return acceptCountVo;
    }

    @Override
    public List<OilSampleDetectionVo> selectOilList(String lmtNumber) {

        List<OilSampleDetectionVo> oilList = oilSampleDetectionMapper.selectOil(lmtNumber, null);
        return oilList;
    }

    @Override
    public List<DrDamperDataVo> selectDrDamperList(String lmtNumber) {

        List<DrDamperDataVo> drDamperDataList = drDamperDataMapper.selectDamperData(null, null, lmtNumber);
        return drDamperDataList;
    }

    @Override
    public TbRepairplanlist selectPlanInfo(Long lmtRepairId) {
        return tbRepairplanlistMapper.selectPlanInfo(lmtRepairId);
    }

    @Override
    public TableDataInfo<TbLmtrepairWorkdetailVo> selectAcceptList(String status, PageQuery pageQuery) {
        List<TbLmtrepairWorkdetailVo> result = new ArrayList<>();
        //找到当前用户
        LoginUser loginUser = LoginHelper.getLoginUser();
        String workUser = loginUser.getUsername();
        String workGroup = loginUser.getDeptName();
        Long workGroupId = loginUser.getDeptId();
        List<Long> deptIds = getDepartmentAndChildren(workGroupId);
        //根据前端传来的参数查询对应的工作表内容
        if (status.equals("所有")) {
            //查询符合条件的工作表内容
            result = tbLmtrepairWorkdetailMapper.selectAllCountList(deptIds);
        } else if (status.equals("未开始")) {
            //查询符合条件的工作表内容
            result = tbLmtrepairWorkdetailMapper.selectNotStartCountList(deptIds);
        } else if (status.equals("进行中")) {
            result = tbLmtrepairWorkdetailMapper.selectNotFinishCountList(deptIds);
        } else if (status.equals("已完成")) {
            result = tbLmtrepairWorkdetailMapper.selectFinishCountList(deptIds);
        } else if (status.equals("已遗留")) {
            result = tbLmtrepairWorkdetailMapper.selectIssuesCountList(deptIds);
        }

        //分页
        if (pageQuery.getPageSize() != null && pageQuery.getPageNum() != null) {
            List<TbLmtrepairWorkdetailVo> page = ListUtil.page(pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);
            TableDataInfo res = new TableDataInfo<>(page, result.size());
            return res;
        } else {
            return TableDataInfo.build(result);
        }
    }

    @Override
    public List<DeptVo> selectDeptVoList() {

        return tbLmtrepairWorkdetailMapper.selectDeptVoList();
    }

    @Override
    public List<DeptVo> selectUserList(Long deptId) {
        return tbLmtrepairWorkdetailMapper.selectUserList(deptId);
    }

    @Override
    public TbLmtrepairWorkdetailVo selectWorkDetailData(String equipmentName, Long lmtRepairId) {
        return tbLmtrepairWorkdetailMapper.selectWorkDetailData(equipmentName, lmtRepairId);
    }

    @Override
    public List<TaskStatsVo> getTaskStats(Date startDate, Date endDate) {
        List<TaskStatsVo> statsList = new ArrayList<>();

        Calendar cal = Calendar.getInstance();
        cal.setTime(startDate);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        while (!cal.getTime().after(endDate)) {
            Date current = cal.getTime();

            int issuedCount = tbLmtrepairWorkdetailMapper.selectIssuedCount(current, DateUtils.addDays(current, 1));
            int completedCount = tbLmtrepairWorkdetailMapper.selectCompletedCount(current, DateUtils.addDays(current, 1));

            TaskStatsVo vo = new TaskStatsVo();
            vo.setDate(current);
            vo.setIssuedCount(issuedCount);
            vo.setCompletedCount(completedCount);
            String dateStr = sdf.format(current);
            vo.setDateStr(dateStr.substring(dateStr.indexOf("-") + 1, dateStr.indexOf(" ")).replace("-", "/"));
            statsList.add(vo);
            cal.add(Calendar.DATE, 1);
        }

        return statsList;
    }

    @Override
    public MyTestCountVO selectTestCount(String id) {
        MyTestCountVO myTestCountVO = new MyTestCountVO();
        //获取未下达任务数
        myTestCountVO.setNotStartCount(tbLmtrepairWorkdetailMapper.selectNotReleased(id));
        //获取已下达任务数
        myTestCountVO.setNotFinishCount(tbLmtrepairWorkdetailMapper.selectReleased(id));
        myTestCountVO.setFinishCount(tbLmtrepairWorkdetailMapper.selectFinishCountById1(id));
        myTestCountVO.setInWorkCount(tbLmtrepairWorkdetailMapper.selectInWorkCount(id));
        return myTestCountVO;
    }

    @Override
    public AcceptCountVo selectAcceptCountById(String id, String workSelect) {
        AcceptCountVo acceptCountVo = new AcceptCountVo();


        if (workSelect.equals("自己的工作")) {
            LoginUser loginUser = LoginHelper.getLoginUser();
            String workUser = loginUser.getUsername();
            //查询符合条件的数量
            acceptCountVo.setAllCount(tbLmtrepairWorkdetailMapper.selectAllCountById(id, workUser, null));
            acceptCountVo.setNotStartCount(tbLmtrepairWorkdetailMapper.selectNotStartCountById(id, workUser, null));
            acceptCountVo.setNotFinishCount(tbLmtrepairWorkdetailMapper.selectNotFinishCountById(id, workUser, null));
            acceptCountVo.setFinishCount(tbLmtrepairWorkdetailMapper.selectFinishCountById(id, workUser, null));

        } else if (workSelect.equals("本工队任务")) {
            LoginUser loginUser = LoginHelper.getLoginUser();
            Long workGroupId = loginUser.getDeptId();
            // 获取工队及其所有子工队的ID列表
            List<Long> deptIds = getDepartmentAndChildren(workGroupId);
            //查询符合条件的数量
            acceptCountVo.setAllCount(tbLmtrepairWorkdetailMapper.selectAllCountById(id, null, deptIds));
            acceptCountVo.setNotStartCount(tbLmtrepairWorkdetailMapper.selectNotStartCountById(id, null, deptIds));
            acceptCountVo.setNotFinishCount(tbLmtrepairWorkdetailMapper.selectNotFinishCountById(id, null, deptIds));
            acceptCountVo.setFinishCount(tbLmtrepairWorkdetailMapper.selectFinishCountById(id, null, deptIds));
        } else if (workSelect.equals("全部任务")) {
            //查询符合条件的数量
            acceptCountVo.setAllCount(tbLmtrepairWorkdetailMapper.selectAllCountById(id, null, null));
            acceptCountVo.setNotStartCount(tbLmtrepairWorkdetailMapper.selectNotStartCountById(id, null, null));
            acceptCountVo.setNotFinishCount(tbLmtrepairWorkdetailMapper.selectNotFinishCountById(id, null, null));
            acceptCountVo.setFinishCount(tbLmtrepairWorkdetailMapper.selectFinishCountById(id, null, null));
        }

        return acceptCountVo;
    }

    @Override
    public TbLmtrepairWorkdetailBo selectByMutual(Long lmtRepairId) {
        return tbLmtrepairWorkdetailMapper.selectByMutual(lmtRepairId);
    }

    @Override
    public TableDataInfo<TbLegacyIssuesVo> selectUserIssuesWorkList(String lmtType, String lmtNumber, String workType, PageQuery pageQuery) {
        //找到当前用户
        LoginUser loginUser = LoginHelper.getLoginUser();
        String workUser = loginUser.getUsername();
        String workGroup = loginUser.getDeptName();
        Long workGroupId = loginUser.getDeptId();
        List<Long> deptIds = getDepartmentAndChildren(workGroupId);

        List<TbLegacyIssuesVo> result = tbLmtrepairWorkdetailMapper.selectUserIssuesWorkList(workUser);
        //分页
        if (pageQuery.getPageSize() != null && pageQuery.getPageNum() != null) {
            List<TbLegacyIssuesVo> page = ListUtil.page(pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);
            TableDataInfo res = new TableDataInfo<>(page, result.size());
            return res;
        } else {
            return TableDataInfo.build(result);
        }
    }


    @Override
    public TableDataInfo<TbLmtrepairWorkdetailVo> selectIndexTest(String lmtType, String lmtNumber, String workType, String workGroup, Date startTime,Date endTime, PageQuery pageQuery) {
        List<TbLmtrepairWorkdetailVo> result = tbLmtrepairWorkdetailMapper.selectIndexTest(lmtType, lmtNumber, workType, workGroup, startTime,endTime);
        //分页
        if (pageQuery.getPageSize() != null && pageQuery.getPageNum() != null) {
            List<TbLmtrepairWorkdetailVo> page = ListUtil.page(pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);
            TableDataInfo res = new TableDataInfo<>(page, result.size());
            return res;
        } else {
            return TableDataInfo.build(result);
        }
    }

    @Override
    public TableDataInfo<TbLmtrepairWorkdetailVo> selectIndexNotTest(String lmtType, String lmtNumber, String workType, String workGroup, Date startTime,Date endTime, PageQuery pageQuery) {
        List<TbLmtrepairWorkdetailVo> result = tbLmtrepairWorkdetailMapper.selectIndexNotTest(lmtType, lmtNumber, workType, workGroup, startTime,endTime);
        //分页
        if (pageQuery.getPageSize() != null && pageQuery.getPageNum() != null) {
            List<TbLmtrepairWorkdetailVo> page = ListUtil.page(pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);
            TableDataInfo res = new TableDataInfo<>(page, result.size());
            return res;
        } else {
            return TableDataInfo.build(result);
        }
    }

    @Override
    public MessageTestVo selectMessage() {
        // 创建一个Calendar实例
        Calendar calendar = Calendar.getInstance();

        // 将时间设置为当前时间
        calendar.setTime(new Date());

        // 减去一天来设置日期为前一天
        calendar.add(Calendar.DATE, -1);

        // 设置时分秒为午夜00:00:00
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        // 从Calendar实例获取修改后的时间
        Date startRequireDate = calendar.getTime();
        //找到当前用户
        MessageTestVo messageTestVo = new MessageTestVo();
        LoginUser loginUser = LoginHelper.getLoginUser();
        String workUser = loginUser.getUsername();
        String workGroup = loginUser.getDeptName();
        //找到未派活任务和未完成任务数量
        //未完成
        String notFinish = tbLmtrepairWorkdetailMapper.selectNotFinish(workUser, startRequireDate);
        //未派活
        String notIssued = tbLmtrepairWorkdetailMapper.selectNotIssued(workGroup, startRequireDate);
        messageTestVo.setNotFinish(notFinish);
        messageTestVo.setNotIssued(notIssued);
        return messageTestVo;
    }

    @Override
    public TbLmtrepairWorkdetailVo selectWorkInfo(Long id) {
        return tbLmtrepairWorkdetailMapper.selectWorkInfo(id);
    }

    @Override
    public TbLmtrepairWorkdetailBo selectWorkInfoBo(Long id) {
        return tbLmtrepairWorkdetailMapper.selectWorkInfoBo(id);
    }

    @Override
    public HistoryDetailVo selectHistoryDetailList(Long lmtRepairId) {
        List<TbRepairplanlistVo> list = tbRepairplanlistMapper.selectWorkList(lmtRepairId);
        List<IssuesVo> list1 = tbLmtrepairWorkdetailMapper.selectIssuesById(lmtRepairId);
        List<FormDataVo> list2 = tbLmtrepairWorkdetailMapper.selectFormDataById(lmtRepairId);
        List<TbWorkFileVo> list3 = tbLmtrepairWorkdetailMapper.selectWorkFileById(lmtRepairId);
        HistoryDetailVo historyDetail = new HistoryDetailVo();
        //任务详情
        historyDetail.setWorkList(list);
        //遗留问题
        historyDetail.setIssuesList(list1);
        //表单详情
        historyDetail.setHistoryList(list2);
        //图片附件
        historyDetail.setWorkFileList(list3);
        return historyDetail;
    }

    @Override
    public JSONArray historyDetail2(Long lmtRepairId) {
        JSONArray resarr = new JSONArray();

        List<FormDataVo> list2 = tbLmtrepairWorkdetailMapper.selectFormDataById(lmtRepairId);
        for (FormDataVo formData : list2) {
            JSONObject jsonObject = JSONUtil.parseObj(formData.getPrintConfig());
            resarr.add(jsonObject);
        }
        return resarr;
    }


    @Override
    public MyTestCountVO selectMyTestVo() {
        MyTestCountVO myTestCountVO = new MyTestCountVO();
        //找到当前用户
        LoginUser loginUser = LoginHelper.getLoginUser();
        String workUser = loginUser.getUsername();
        myTestCountVO.setNotStartCount(tbLmtrepairWorkdetailMapper.selectNotStartVo(workUser));
        myTestCountVO.setNotFinishCount(tbLmtrepairWorkdetailMapper.selectNotFinishVo(workUser));

        // 创建一个Calendar实例
        Calendar calendar = Calendar.getInstance();

        // 将时间设置为当前时间
        calendar.setTime(new Date());

        // 减去一天来设置日期为前1天
        calendar.add(Calendar.DATE, -1);

        // 设置时分秒为午夜00:00:00
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        // 从Calendar实例获取修改后的时间
        Date startRequireDate = calendar.getTime();

        myTestCountVO.setIncompleteCount(tbLmtrepairWorkdetailMapper.selectMessageCount(workUser, startRequireDate));
        myTestCountVO.setIssuesCount(tbLmtrepairWorkdetailMapper.selectUserIssuesCount(workUser));

        return myTestCountVO;
    }

    @Override
    public TableDataInfo<TbLmtrepairWorkdetailVo> selectIssuesWorkList(String lmtType, String lmtNumber, String workType, PageQuery pageQuery) {

        // 创建一个Calendar实例
        Calendar calendar = Calendar.getInstance();

        // 将时间设置为当前时间
        calendar.setTime(new Date());

        // 减去一天来设置日期为前1天
        calendar.add(Calendar.DATE, -1);

        // 设置时分秒为午夜00:00:00
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        // 从Calendar实例获取修改后的时间
        Date startRequireDate = calendar.getTime();

        //找到当前用户
        LoginUser loginUser = LoginHelper.getLoginUser();
        String workUser = loginUser.getUsername();

        List<TbLmtrepairWorkdetailVo> result = tbLmtrepairWorkdetailMapper.selectIssuesWorkList(lmtType, lmtNumber, workType, workUser, startRequireDate);
        //分页
        if (pageQuery.getPageSize() != null && pageQuery.getPageNum() != null) {
            List<TbLmtrepairWorkdetailVo> page = ListUtil.page(pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);
            TableDataInfo res = new TableDataInfo<>(page, result.size());
            return res;
        } else {
            return TableDataInfo.build(result);
        }
    }

    @Override
    public TableDataInfo<TbLmtrepairWorkdetailVo> selectLegacyById(String lmtRepairId, String lmtType, String lmtNumber, String status, String repairChar, PageQuery pageQuery) {
        List<TbLmtrepairWorkdetailVo> result = tbLmtrepairWorkdetailMapper.selectLegacyById(lmtRepairId, lmtType, lmtNumber, status, repairChar);
        //分页
        if (pageQuery.getPageSize() != null && pageQuery.getPageNum() != null) {
            List<TbLmtrepairWorkdetailVo> page = ListUtil.page(pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);
            TableDataInfo res = new TableDataInfo<>(page, result.size());
            return res;
        } else {
            return TableDataInfo.build(result);
        }
    }

    @Override
    public TbDigitalLmtDataVo selectLmtData() {

        TbRepairplanVo tbRepairplanVo = tbRepairplanMapper.selectNowList();
        Long planId = tbRepairplanVo.getId();
        TbDigitalLmtDataVo tbDigitalLmtDataVo = new TbDigitalLmtDataVo();
        tbDigitalLmtDataVo.setFinishCount(String.valueOf(tbRepairplanlistMapper.selectLmtFinish(planId)));
        tbDigitalLmtDataVo.setNotFinishCount(tbRepairplanlistMapper.selectLmtNotFinish(planId));
        tbDigitalLmtDataVo.setNotStartCount(tbRepairplanlistMapper.selectLmtNotStart(planId));
        tbDigitalLmtDataVo.setLmtNumberCount(iTbLocomotiveService.countAll() + "");
        tbDigitalLmtDataVo.setLmtTypeCount(iTbLmttypeService.countAll() + "");
        return tbDigitalLmtDataVo;
    }

    @Override
    public EquipmentCountVO selectEquipmentCount() {
        EquipmentCountVO equipmentCountVO = new EquipmentCountVO();
        equipmentCountVO.setInCount(tbStorageMapper.selectInCount());
        equipmentCountVO.setOutCount(tbStorageMapper.selectOutCount());
        equipmentCountVO.setAllCount(equipmentMapper.selectAllCount());
        return equipmentCountVO;
    }

    @Override
    public TbLmtrepairWorkdetailBo selectBySpecial(Long lmtRepairId) {
        return tbLmtrepairWorkdetailMapper.selectBySpecial(lmtRepairId);
    }

    @Override
    public TbLmtrepairWorkdetailBo selectByAcceptance(Long lmtRepairId) {
        return tbLmtrepairWorkdetailMapper.selectByAcceptance(lmtRepairId);
    }

    @Override
    public TableDataInfo<TbLmtrepairWorkdetailVo> selectWorkList1(String status, String workKind, String workCategory, String workName, String workRequire, String workGroup, String workType, PageQuery pageQuery) {
        //查询符合条件的工作表内容
        List<TbLmtrepairWorkdetailVo> result = tbLmtrepairWorkdetailMapper.selectWorkByRepairId1(status, workKind, workCategory, workName, workRequire, workGroup, workType);
        //分页
        for (TbLmtrepairWorkdetailVo vo : result) {
            //TODO vo.setWorkHeight(getWorkHeight(vo.getFillData()));
            vo.setWorkHeight(getWorkHeightObject());
        }
        if (pageQuery.getPageSize() != null && pageQuery.getPageNum() != null) {
            List<TbLmtrepairWorkdetailVo> page = ListUtil.page(pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);

            TableDataInfo res = new TableDataInfo<>(page, result.size());

            return res;
        } else {
            return TableDataInfo.build(result);
        }
    }

    private Map<String, Object> getWorkHeight(String jsonStr) {
        if (StringUtils.isNotBlank(jsonStr) && !"0".equals(jsonStr)) {
            com.alibaba.fastjson.JSONObject entity = JSON.parseObject(jsonStr);
            Map<String, Object> params = new HashMap<>();
            params.put("frontLeftFreeHeight", entity.get("front_left_free_height") == null ? null : entity.getString("front_left_free_height"));
            params.put("frontRightFreeHeight", entity.get("front_right_free_height") == null ? null : entity.getString("front_right_free_height"));
            params.put("frontFreeHeightBad", entity.get("front_free_height_bad") == null ? null : entity.getString("front_free_height_bad"));
            params.put("frontLeftCompressionAmount", entity.get("front_left_compression_amount") == null ? null : entity.getString("front_left_compression_amount"));
            params.put("frontRightCompressionAmount", entity.get("front_right_compression_amount") == null ? null : entity.getString("front_right_compression_amount"));
            params.put("frontLeftWearPlateThickness", entity.get("front_left_wear_plate_thickness") == null ? null : entity.getString("front_left_wear_plate_thickness"));
            params.put("frontRightWearPlateThickness", entity.get("front_right_wear_plate_thickness") == null ? null : entity.getString("front_right_wear_plate_thickness"));
            params.put("frontRightWearAmount", entity.get("front_right_wear_amount") == null ? null : entity.getString("front_right_wear_amount"));
            params.put("frontLeftWearAmount", entity.get("front_left_wear_amount") == null ? null : entity.getString("front_left_wear_amount"));
            params.put("afterLeftFreeHeight", entity.get("after_left_free_height") == null ? null : entity.getString("after_left_free_height"));
            params.put("afterRightFreeHeight", entity.get("after_right_free_height") == null ? null : entity.getString("after_right_free_height"));
            params.put("afterFreeHeightBad", entity.get("after_free_height_bad") == null ? null : entity.getString("after_free_height_bad"));
            params.put("afterLeftCompressionAmount", entity.get("after_left_compression_amount") == null ? null : entity.getString("after_left_compression_amount"));
            params.put("afterRightCompressionAmount", entity.get("after_right_compression_amount") == null ? null : entity.getString("after_right_compression_amount"));
            params.put("afterLeftWearPlateThickness", entity.get("after_left_wear_plate_thickness") == null ? null : entity.getString("after_left_wear_plate_thickness"));
            params.put("afterRightWearPlateThickness", entity.get("after_right_wear_plate_thickness") == null ? null : entity.getString("after_right_wear_plate_thickness"));
            params.put("afterRightWearAmount", entity.get("after_right_wear_amount") == null ? null : entity.getString("after_right_wear_amount"));
            params.put("afterLeftWearAmount", entity.get("after_left_wear_amount") == null ? null : entity.getString("after_left_wear_amount"));
            return params;
        }
        return null;
    }

    private Map<String, Object> getWorkHeightObject() {
        Map<String, Object> params = new HashMap<>();
        params.put("frontLeftFreeHeight", "131");
        params.put("frontRightFreeHeight", "133");
        params.put("frontFreeHeightBad", "2");
        params.put("frontLeftCompressionAmount", "12");
        params.put("frontRightCompressionAmount", "11");
        params.put("frontLeftWearPlateThickness", "11");
        params.put("frontRightWearPlateThickness", "10");
        params.put("frontRightWearAmount", "1");
        params.put("frontLeftWearAmount", "2");


        params.put("afterLeftFreeHeight", "133");
        params.put("afterRightFreeHeight", "133");
        params.put("afterFreeHeightBad", "0");
        params.put("afterLeftCompressionAmount", "11");
        params.put("afterRightCompressionAmount", "11");
        params.put("afterLeftWearPlateThickness", "10");
        params.put("afterRightWearPlateThickness", "10");
        params.put("afterRightWearAmount", "2");
        params.put("afterLeftWearAmount", "2");
        return params;
    }

    @Override
    public TableDataInfo<TbLmtrepairWorkdetailVo> listWheelDiameter(TbLmtrepairWorkdetailBo bo, PageQuery pageQuery) {

        LambdaQueryWrapper<TbLmtrepairWorkdetail> lqw = buildQueryWrapper(bo);
        lqw.like(TbLmtrepairWorkdetail::getFormConfig, "轮径");
        //lqw.eq(TbLmtrepairWorkdetail::getStatus,"已完成");
        Page<TbLmtrepairWorkdetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        for (TbLmtrepairWorkdetailVo vo : result.getRecords()) {
            vo.setWheelDiameterDataVoList(getWheelDiameterDataList(vo.getFillData()));
            if (StringUtils.isBlank(vo.getLmtType())) {
                TbRepairplanlistVo tbRepairplanlistVo = iTbRepairplanlistService.queryById(vo.getLmtRepairId());
                if (tbRepairplanlistVo != null) {
                    vo.setLmtType(tbRepairplanlistVo.getLmtType());
                    vo.setLmtNumber(tbRepairplanlistVo.getLmtNumber());
                }
            }

        }
        return TableDataInfo.build(result);
    }

    private String getNameInJson(String name, String fillData) {
        com.alibaba.fastjson.JSONObject entity = JSON.parseObject(fillData);
        return entity.get(name) == null ? null : entity.getString(name);
    }

    @Override
    public int countByWeekAndWorkType(LocalDate date, String workType) {
        return baseMapper.countByWeekAndWorkType(date, workType);
    }

    @Override
    public TbLmtrepairWorkdetailVo selectByLmtTypeLmtNumberWorkCategoryWorkCategoryId(String lmtType, String lmtNumber, String workCategory, String workCategoryId, String status) {
        return baseMapper.selectByLmtTypeLmtNumberWorkCategoryWorkCategoryId(lmtType, lmtNumber, workCategory, workCategoryId, status);
    }

    @Override
    public TbLmtrepairWorkdetailVo selectTopOneByLmtNumberAndRepairChar(String lmtNumber, String workType) {
        return baseMapper.selectTopOneByLmtNumberAndRepairChar(lmtNumber, workType);
    }


    public List<WheelDiameterVo> wheelDiameterList = null;

    @Override
    public List<WheelDiameterDataVo> getWheelDiameterDataList(String jsonStr) {
        if (wheelDiameterList == null) {
            wheelDiameterList = getWheelDiameterList(50);
        }
        List<WheelDiameterDataVo> wheelDiameterDataVoList = new ArrayList<>();
        if (StringUtils.isNotBlank(jsonStr) && !"0".equals(jsonStr)) {
            com.alibaba.fastjson.JSONObject entity = JSON.parseObject(jsonStr);
            for (WheelDiameterVo wheelDiameterVo : wheelDiameterList) {
                System.out.println(wheelDiameterVo.getLeft1Val());
                if (StringUtils.isBlank(entity.getString(wheelDiameterVo.getLeft1Val()))) {
                    break;
                }
                WheelDiameterDataVo wheelDiameterDataVo = new WheelDiameterDataVo();
                wheelDiameterDataVo.setLeft1Name(wheelDiameterVo.getLeft1Name());
                wheelDiameterDataVo.setLeft2Name(wheelDiameterVo.getLeft2Name());
                wheelDiameterDataVo.setRight1Name(wheelDiameterVo.getRight1Name());
                wheelDiameterDataVo.setRight2Name(wheelDiameterVo.getRight2Name());
                wheelDiameterDataVo.setLeft1Val(StringUtils.isBlank(entity.getString(wheelDiameterVo.getLeft1Val()))?"0":entity.getString(wheelDiameterVo.getLeft1Val()));
                wheelDiameterDataVo.setLeft2Val(StringUtils.isBlank(entity.getString(wheelDiameterVo.getLeft2Val()))?"0":entity.getString(wheelDiameterVo.getLeft2Val()));
                wheelDiameterDataVo.setRight1Val(StringUtils.isBlank(entity.getString(wheelDiameterVo.getRight1Val()))?"0":entity.getString(wheelDiameterVo.getRight1Val()));
                wheelDiameterDataVo.setRight2Val(StringUtils.isBlank(entity.getString(wheelDiameterVo.getRight2Val()))?"0":entity.getString(wheelDiameterVo.getRight2Val()));
                wheelDiameterDataVoList.add(wheelDiameterDataVo);
            }
        }
        /*if (wheelDiameterDataVoList.size() == 0) {
            List<WheelDiameterVo> wheelDiameterList1 = getWheelDiameterList(10);
            for (WheelDiameterVo wheelDiameterVo : wheelDiameterList1) {
                WheelDiameterDataVo wheelDiameterDataVo = new WheelDiameterDataVo();
                wheelDiameterDataVo.setLeft1Name(wheelDiameterVo.getLeft1Name());
                wheelDiameterDataVo.setLeft2Name(wheelDiameterVo.getLeft2Name());
                wheelDiameterDataVo.setRight1Name(wheelDiameterVo.getRight1Name());
                wheelDiameterDataVo.setRight2Name(wheelDiameterVo.getRight2Name());
                wheelDiameterDataVo.setLeft1Val("12");
                wheelDiameterDataVo.setLeft2Val("12");
                wheelDiameterDataVo.setRight1Val("12");
                wheelDiameterDataVo.setRight2Val("12");
                wheelDiameterDataVoList.add(wheelDiameterDataVo);
            }
        }*/
        return wheelDiameterDataVoList;
    }

    @Override
    public void updateByRepairIdsAndCategoryAndKind(TbLmtrepairWorkdetail workdetail, List<Long> repairIds) {
        baseMapper.updateByRepairIdsAndCategoryAndKind(workdetail,repairIds);
    }
    @Override
    public void updateByRepairIdsAndCategoryAndKind1(TbLmtrepairWorkdetail workdetail, List<Long> repairIds) {
        baseMapper.updateByRepairIdsAndCategoryAndKind1(workdetail,repairIds);
    }

    @Override
    public List<TbLmtrepairWorkdetailVo> selectIndexTestExport(String lmtType, String lmtNumber, String workType, String workGroup, Date startTime ,Date endTime) {
        return tbLmtrepairWorkdetailMapper.selectIndexTest(lmtType, lmtNumber, workType, workGroup, startTime,endTime);
    }

    @Override
    public List<TbLmtrepairWorkdetailVo> selectIndexNotTestExport(String lmtType, String lmtNumber, String workType, String workGroup, Date startTime ,Date endTime) {
        return tbLmtrepairWorkdetailMapper.selectIndexNotTest(lmtType, lmtNumber, workType, workGroup, startTime,endTime);
    }

    @Override
    public void deleteByLmtRepairId(Long id) {
        tbLmtrepairWorkdetailMapper.deleteByLmtRepairId(id);;
    }

    @Override
    public List<TbLmtrepairWorkdetailVo> queryByLmtRepairId(Long id) {
        return tbLmtrepairWorkdetailMapper.queryByLmtRepairId(id);
    }

    public List<WheelDiameterVo> getWheelDiameterList(int num) {
        List<WheelDiameterVo> wheelDiameterVoList = new ArrayList<WheelDiameterVo>();
        for (int i = 1; i < num + 1; i++) {
            WheelDiameterVo wheelDiameterVo = new WheelDiameterVo();
            wheelDiameterVo.setLeft1Val("wheelDiameter" + i + "Left1");
            wheelDiameterVo.setLeft2Val("wheelDiameter" + i + "Left2");
            wheelDiameterVo.setRight1Val("wheelDiameter" + i + "Right1");
            wheelDiameterVo.setRight2Val("wheelDiameter" + i + "Right2");
            wheelDiameterVo.setLeft1Name("轮径" + i + "轴左1");
            wheelDiameterVo.setLeft2Name("轮径" + i + "轴左2");
            wheelDiameterVo.setRight1Name("轮径" + i + "轴右1");
            wheelDiameterVo.setRight2Name("轮径" + i + "轴右2");
            wheelDiameterVoList.add(wheelDiameterVo);
        }
        return wheelDiameterVoList;
    }

}
